<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        class="filter-item"
        v-model="timeTo"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions"
        @change="changeTime">
      </el-date-picker>
      <el-select v-model="listQuery.td"
                 style="width: 140px"
                 class="filter-item"
                 placeholder="选择激活状态"
                 @change="handleFilter">
        <el-option v-for="item in tdOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-select v-model="skillHouse"
                 style="width: 140px"
                 class="filter-item"
                 placeholder="出版社"
                 @change="handleFilter">
        <el-option v-for="item in cbsOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-select v-model="skillType"
                 style="width: 140px"
                 class="filter-item"
                 placeholder="类型"
                 @change="handleFilter">
        <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>

      <el-input v-model="listQuery.condition" placeholder="搜索或选择客户" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <!--<el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">Add</el-button>-->
      <el-button :loading="downloadLoading"
                 class="filter-item"
                 type="primary"
                 icon="el-icon-download"
                 @click="handleDownload">导出
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      @sort-change="sortChange"
    >
      <el-table-column label="ID" prop="id" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备ID" width="180px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.deviceSeries}}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户标签" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.appIsbn}}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.clientName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="出版社" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.skillHouse}}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.skillType}}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.price}}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册信息" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.registerTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="激活状态" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.skillStatus}}</span>
        </template>
      </el-table-column>
      <el-table-column label="激活时间" min-width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime | timeString}}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期时间" min-width="120px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.overTime | timeString}}</span>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="row flex-end"
      style="margin-top: 20px"
      v-show="total>0"
      background
      layout="total, sizes, prev, pager, next"
      :page-sizes="[20, 30, 40, 50]"
      :total="total"
      @size-change="sizeChange"
      @current-change="pageChange">
    </el-pagination>
  </div>
</template>

<script>
import { downloadFile, toLocalTime } from '../../utils/common'

export default {
  name: 'copyrightIndex',
  filters: {
    timeString: function (val) {
      if (val > 0) {
        let d = new Date(val)
        return toLocalTime(d)
      } else {
        return ''
      }
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        td: '', // 激活状态，1代表已激活，2代表已失效
        condition: '', // 搜索
        page: 1, // 页码
        limit: 10, // 数量
        skillType: '', // 类型
        skillHouse: '', // 出版社
        staTime: '', // 起始日期
        enTime: '', // 截止日期
      },

      skillType: '',
      skillHouse: '',
      tdOptions: [
        { value: 0, label: '全部' },
        { value: 1, label: '已激活' },
        { value: 2, label: '已失效' },
      ],
      cbsOptions: [],
      typeOptions: [],

      timeTo: '',
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },

      downloadLoading: false, // 是否正在下载
      upload_loading: false, // 是否正在上传
    }
  },
  created() {
    this.getOptions()
    this.getList()
  },
  methods: {
    getOptions() {
      this.apiGet('/api/manage2/selectordata').then(res => {
        if (res.code === 0) {
          let cbs = res.data.skillHouseList || []

          cbs.forEach((m, i) => {
            this.cbsOptions.push({
              value: i,
              label: i === 0 ? '全部' : m,
              text: i === 0 ? '' : m,
            })
          })
          let type = res.data.skillTypeList || []
          type.forEach((m, i) => {
            this.typeOptions.push({
              value: i,
              label: i === 0 ? '全部' : m,
              text: i === 0 ? '' : m,
            })
          })
        }
      })
    },
    sizeChange(limit) {
      this.listQuery.limit = limit
      this.getList()
    },
    pageChange(page) {
      this.listQuery.page = page
      this.getList()
    },
    // 获取数据
    getList() {
      this.listLoading = true

      if (this.skillType !== '') {
        this.listQuery.skillType = this.typeOptions[this.skillType].text
      }
      if (this.skillHouse !== '') {
        this.listQuery.skillHouse = this.cbsOptions[this.skillHouse].text
      }

      this.apiGet('/api/manage2/list', {
        ...this.listQuery
      }).then(res => {
        this.listLoading = false

        if (res.code === 0) {
          this.list = res.data.list
          this.total = res.data.totalCount
        } else {
          this.$message.warning(res.message)
        }

      }).catch(() => {
        this.listLoading = false

      })
    },
    changeTime() {
      this.listQuery.staTime = toLocalTime(this.timeTo[0])
      let d = new Date(this.timeTo[1].getTime() + 86399000)
      this.listQuery.enTime = toLocalTime(d)
      this.handleFilter()
    },
    // 筛选条件变动，重新获取列表数据
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 排序
    sortChange(data) {
      console.log(data);
    },
    // 导出 Excel
    handleDownload() {
      if (this.downloadLoading) {
        return
      }
      this.downloadLoading = true

      if (this.skillType !== '') {
        this.listQuery.skillType = this.typeOptions[this.skillType].text
      }
      if (this.skillHouse !== '') {
        this.listQuery.skillHouse = this.cbsOptions[this.skillHouse].text
      }

      this.apiPost('/api/manage2/exportThem', {
        ...this.listQuery
      }).then(res => {
        if (res.code === 0) {
          downloadFile(res.data + '&token=' + localStorage.getItem('token'))
        }
      }).catch(() => {
        // this.$message.warning('')
      }).finally(() => {
        this.downloadLoading = false
      })
    },
  }
}
</script>

<style lang="scss" scoped>
// 筛选
.filter-container {
  background: #fff;
  border: 1px solid #DCDFE6;
  padding: 10px;
  .filter-item {
    vertical-align: middle;
    margin-bottom: 10px;
  }
}

.filter-item {
  margin-right: 10px;
}

.text-span {
  padding: 0 5px;
  cursor: pointer;
  border-bottom: 1px solid #ccc;
  /*text-decoration: underline;*/
}

.btn-action {
  margin: 3px 3px;
}
</style>
